﻿using Telerik.Web.UI;
using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web;
using System.IO;
using System.IO.Compression;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NCC_SOP
{
    public partial class test : System.Web.UI.Page
    {
        protected void btnInitInsert_Click(object sender, System.EventArgs e)
        {
        }
        protected string NormalizeValue(object inputValue)
        {
            return inputValue.ToString().Replace(" ", "");
        }
 
        protected void Page_Init(object sender, EventArgs e)
        {
 
            GridFilterMenu filterMenu = RadGrid1.FilterMenu;
 
            int currentItemIndex = 0;
            while (currentItemIndex < filterMenu.Items.Count)
            {
                RadMenuItem item = filterMenu.Items[currentItemIndex];
                if (item.Text.Contains("Empty") || item.Text.Contains("Null"))
                {
                    filterMenu.Items.Remove(item);
                }
                else currentItemIndex++;
            }
        }
 
        protected string GetCarImageUrl(object container)
        {
            Hashtable values = new Hashtable();
            (container as GridNestedViewItem).ParentItem.ExtractValues(values);
            return String.Format("~/Grid/Examples/Overview/Images/{0}_{1}.jpg", NormalizeValue(values["BrandName"]), NormalizeValue(values["Model"]));
        }
 
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" || e.CommandName == "ExpandCollapse")
            {
                bool lastState = e.Item.Expanded;
 
                if (e.CommandName == "ExpandCollapse")
                {
                    lastState = !lastState;
                }
 
                CollapseAllRows();
                e.Item.Expanded = !lastState;
            }
            if (e.CommandName == "UpdateCount")
            {
            }
        }
 
        private void CollapseAllRows()
        {
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                item.Expanded = false;
            }
        }
 
        protected void BrandNameCombo_DataBound(object sender, EventArgs e)
        {
            RadComboBox combo = sender as RadComboBox;
            foreach (RadComboBoxItem item in combo.Items)
            {
                item.ImageUrl = String.Format("~/Grid/Examples/Overview/Images/SmallLogos/{0}.gif", NormalizeValue(item.Text));
            }
        }
 
        protected void SaveSettingsButton_Click(object sender, EventArgs e)
        {
        }
 
        protected void LoadSettingsButton_Click(object sender, EventArgs e)
        {
            RadGrid1.Rebind();
        }

        public void RunStoredProc()
        {
            using (var conn = new SqlConnection("MembershipString"))
            // "dbo.name" or "name"
            using (var command = new SqlCommand("dbo.getDataSources_byTableName", conn)
            {
                CommandType = CommandType.StoredProcedure
                // where to put the parameters?
            })
            {
                conn.Open();
                command.ExecuteNonQuery();
                // how to set parameter?
                command.Parameters.Add("@tableName" , SqlDbType.VarChar, 50);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // read parameters from the database
                        // how to connect to the UI and show on the table?
                    }
                }
                conn.Close();
            }
        }

       /* protected void RadGrid1_PreRender(object sender, System.EventArgs e)
        {
            if (this.RadGrid1.MasterTableView.FilterExpression != string.Empty)
            {
                RefreshCombos();
            }


        }


        protected void RefreshCombos()
        {
            SqlDataSource1.SelectCommand = SqlDataSource1.SelectCommand + " WHERE " + RadGrid1.MasterTableView.FilterExpression.ToString();
            SqlDataSource2.SelectCommand = SqlDataSource2.SelectCommand + " WHERE " + RadGrid1.MasterTableView.FilterExpression.ToString();
            RadGrid1.MasterTableView.Rebind();
        }*/
 
    }
 
}